package mornd.custom;

import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: mornd
 * @dateTime: 2023/5/9 - 17:05
 * 自定义处理器适配器（真正执行目标方法）
 */
public class MyHandlerAdapter implements HandlerAdapter {
    @Override
    public boolean supports(Object handler) {
        return (handler instanceof Controller);
    }

    @Override
    public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Controller controller = (Controller) handler;
        // 正式调用 controller 的目标方法
        return controller.handleRequest(request, response);
    }

    /**
     * 处理 304 请求，已过时
     * @param request current HTTP request
     * @param handler the handler to use
     * @return
     */
    @Override
    public long getLastModified(HttpServletRequest request, Object handler) {
        return 0;
    }
}
